<?php
/**
 * 微信支付
 * User: liujb
 * Date: 2017/2/16
 * Time: 9:01
 */

namespace Api\Controller;
//include 'wechatAppPay.class.php';

class WechatController  extends ApiController
{
    public  function  wechpay(){
        !api_login() && response(-94);
        !passed_login() && response(-95);
        !only_login() && response(-96);

        params_exist(array('invite_id'));
        $invite_id  =$_POST['invite_id'];

        $invite = M('invite')->where('id='.$invite_id)->find();

        if($invite){
            $amount = M('invite_fee')->getField('money');
        }else{
            response('-9','','没有查到发布的需求');
        }
        import('Vendor.Weixinpay.WechatAppPay');
        //统一下单方法
        $wechatAppPay = new \wechatAppPay();
        $params['body'] = '支付订单';						//商品描述
        $trade_no = build_order_no();
        $params['out_trade_no'] = $trade_no;	//自定义的订单号
        $params['total_fee'] = $amount*100;					//订单金额 只能为整数 单位为分
        $params['trade_type'] = 'APP';					//交易类型 JSAPI | NATIVE | APP | WAP
        $result = $wechatAppPay->unifiedOrder( $params );
        //print_r($result);
        //创建APP端预支付参数
        /** @var TYPE_NAME $result */
        $wxdata = @$wechatAppPay->getAppPayParams($result['prepay_id'] );
        //print_r ($wxdata);
        $data = array(
            'uid'           =>  API_UID,
            'order_no'     => $trade_no,
            'amount'       =>$amount,
            'status'       => 0,
            'create_time'     => NOW_TIME,
            'invite_id'    =>$invite_id,
            'type'          => 2
        );

        $recharge = M('recharge_order');
        $InsertId   = $recharge->add($data);
        if ($InsertId){
            response(200,$wxdata,'');
        }else{
            response(-2);
        }
       // $InsertId   = $recharge->add($data);

    }

    /**
     * 微信支付通知接口
     */
    public function notify(){
        import('Vendor.Weixinpay.WechatAppPay');
        $wechatAppPay = new \wechatAppPay();
        $data = $wechatAppPay->getNotifyData();
        file_put_contents('wx.txt', json_encode($data));
        if ($data) {
            $res['trade_no']=$_POST['out_trade_no'];
            $res['total_fee']=$_POST['total_amount'];
            $order_no=$res['trade_no'];
            $recharge = M('recharge_order')->where(array('order_no' => $order_no))->find();
            //$pay_status=M('recharge_order')->where(array('order_no' => $order_no))->getField('status');
            if($res['total_fee'] !=$recharge['amount'] ){
                exit;
            }

            if($recharge['status']){
                echo 'SUCCESS';
                exit;
            }

            //增加用户 惠Go币
            M('member')->where('uid='.$recharge['uid'])->setInc('amount',$recharge['amount']);
            //更新充值记录
            $map['status'] =1;
            $map['update_time'] =time();
            M('recharge_order')->where(array('order_no' => $order_no))->save($map);

            //邀请奖励处理
            $member = M('member')->where('uid='.$recharge['uid'])->find();
            if($member['referrer']) {
                $refer = M('member')->where('uid='.$member['referrer'])->find();
                if($refer){
                    $coin=$recharge['amount']*10;
                    M('member')->where('uid='.$member['referrer'])->setInc('coin',$coin);
                    //邀请奖励记录
                    $refer_data['uid']=$member['referrer'];
                    $refer_data['order_no']=$order_no;
                    $refer_data['type']   = 5;
                    $refer_data['money']  =$coin;
                    $refer_data['month']  = date('Ym',time());
                    $refer_data['status'] = 1;
                    $refer_data['b_refer'] = $recharge['uid'];
                    $refer_data['createtime']=time();
                    $re = M('user_trade')->add($refer_data);
                    $result['order_no'] = $order_no;
                }
            }
            //接收通知成功后应答输出
            $wechatAppPay->replyNotify();

        }
    }
}